/*
 * @Author: silence
 * @Date: 2022-05-14 14:36:10
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-03-10 11:57:29
 * @Description: 搜索相关接口
 */

import '../../../api/R.dart';
import '../../../config/AppInfo.dart';
import '../../../entities/GameEntity.dart';
import 'package:sil_utils_package/sil_utils_package.dart';

import 'ApiUtils.dart';

/**
 * @description: 搜索应用列表
 * @param {
 * keywords 关键字
 * tags 标签
 * }
 * @return {*}
 */
Future<R> api_search_apps(
    {String? keywords, int? page = 1, String? tags}) async {
  Map<String, dynamic> map = Map();
  map['page'] = page;
  map['keywords'] = keywords ?? "";
  map['limit'] = PageLimit;
  map['tags'] = tags ?? "";

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, '/api/v1.arcade/common/search',
      param: map);

  R r = R.fromData(methodAwait, handlerData: (map) {
    List<GameEntity> dataList = [];
    if (!SilTools.listIsEmpty(map)) {
      for (Map<String, dynamic> item in map as List) {
        dataList.add(GameEntity.fromJson(item));
      }
    }
    return dataList;
  });

  return r;
}

/**
 * @description: 热门搜索
 * @return {*}
 */
Future<R> api_search_hotsearch() async {
  Map<String, dynamic> map = Map();
  map['page'] = 1;
  map['limit'] = PageLimit;

  dynamic methodAWait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, '/api/v1.arcade/common/hotsearch',
      param: map);

  R r = R.fromData(
    methodAWait,
    handlerData: (map) {
      List<GameEntity> dataList = [];
      if (!SilTools.listIsEmpty(map)) {
        for (var item in map) {
          dataList.add(GameEntity.fromJson(item));
        }
      }
      return dataList;
    },
  );
  return r;
}

/**
 * @description: 热词
 * @return {*}
 */
Future<R> api_search_hotword() async {
  Map<String, dynamic> map = Map();
  map['page'] = 1;
  map['limit'] = PageLimit;

  dynamic methodAwait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, '/api/v1.arcade/common/hotword',
      param: map);

  R r = R.fromData(
    methodAwait,
  );

  return r;
}

/**
 * @description: 关键字搜索关联(联想词)
 * @return {
 * keywords 关键字
 * tags 标签
 * }
 */
Future<R> api_search_relevancy(String keywords, {String tags = ''}) async {
  Map<String, dynamic> map = Map();
  map['page'] = 1;
  map['limit'] = PageLimit;
  map['keywords'] = keywords;
  map['tags'] = tags;

  dynamic methodAWait = await ApiUtils.methodAwait(
      ApiUtils.PostMethd, '/api/v1.arcade/common/relevancy',
      param: map);

  R r = R.fromData(
    methodAWait,
    handlerData: (map) {
      List<GameEntity> dataList = [];
      if (!SilTools.listIsEmpty(map)) {
        for (var item in map) {
          dataList.add(GameEntity.fromJson(item));
        }
      }
      return dataList;
    },
  );
  return r;
}
